import 'dart:async';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_soundmagus/model/data_mng.dart';
import 'package:flutter_soundmagus/page/save/save_controller.dart';
import 'package:flutter_soundmagus/widget/alert/alert.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

class HomeController extends GetxController {
  ///总音量，现有2组
  final volumes = [0.0.obs, 0.0.obs];

  ///预设0-3
  final preset = 0.obs;

  ///音源
  final audioSource = AudioSource.BT.obs;

  ///设置音源
  void setAudioSource(AudioSource audioSource) {
    var si = [AudioSource.HiLevel, AudioSource.BT, AudioSource.Aux];
    var i = si.indexOf(audioSource);
    if (i < 0) {
      EasyLoading.showToast('暂不支持此音源');
      return;
    }

    Msg.msgAudioSource(audioSource).send();
    this.audioSource.value = audioSource;
  }

  ///设置预设
  void setPreset(int preset) {
    this.preset.value = preset;
  }

  ///更新组音量
  void updateVolume({required int volGroup, double volume = 0}) {
    volumes[volGroup].value = volume;
  }

  // static final links = [
  //   "https://www.facebook.com/powerbassusainc/",
  //   "https://twitter.com/powerbassusa",
  //   "https://www.instagram.com/powerbassusa/?hl=en",
  //   "https://www.youtube.com/user/PowerbassCarAudio?app=desktop",
  //   "https://www.powerbassusa.com",
  // ];
  static const links = ['https://powerbassusa.com/media/uploads_file/2024/06/05/a6660b906473de0.21919817.pdf'];

  ///点击链接
  void onLink(int i) {
    launchUrl(Uri.parse(links[i]));
  }

  ///重置密码
  Timer? _resetTimer;
  int clickNum = 0;
  void onResetPwdClick() {
    _resetTimer?.cancel(); // 取消之前的计时器
    clickNum += 1;

    if (clickNum == 6) {
      clickNum = 0;
      Get.dialog(
        AlertResetPwd(
          onPressed: () {
            Fluttertoast.showToast(msg: 'Password reset success');
            Get.back();
            Get.find<SaveController>().resetPwd();
          },
        ),
        barrierDismissible: false,
      );
    } else {
      // 每次点击都重新设置4秒后清零
      _resetTimer = Timer(Duration(seconds: 4), () {
        clickNum = 0;
      });
    }
  }
}
